package com.wk.springboot.httpUtils;

import java.io.IOException;
import java.net.URI;
import org.apache.commons.lang.StringUtils;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.assertj.core.util.Preconditions;

/**
 * @Classname DrspHttpGet
 * @Description DrspHttpGet
 * @Date 2020/8/7 11:15
 * @Author by weikai
 */
public class DrspHttpGet extends DrspHttpMethod {

    private HttpGet httpget = new HttpGet();

    public DrspHttpGet(CloseableHttpClient httpClient) {
        super(httpClient);
    }

    public DrspHttpResponse execute() throws ClientProtocolException, IOException {
        this.httpget.setURI(URI.create(this.url));
        return new DrspHttpResponse(this.httpClient.execute(httpget));
    }

    /**
     * 释放连接
     */
    @Override
    public void releaseConnection() {
        this.httpget.releaseConnection();
    }

    public DrspHttpMethod setContentType(String contentType) {
        this.contentType = contentType;
        if (StringUtils.isNotBlank(contentType)) {
            httpget.addHeader("Content-Type", contentType + ";charset=UTF-8");
        }
        return this;
    }

    public DrspHttpGet setUrl(String url) {
        Preconditions.checkArgument(StringUtils.isNotBlank(url), "http调用时url为空");
        this.url = url;
        return this;
    }

}
